package com.origin.onlinesys.service.modules.interviewsys.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.origin.onlinesys.common.services.BaseService;
import com.origin.onlinesys.domain.modules.interviewsys.InterviewerInfo;
import com.origin.onlinesys.domain.modules.interviewsys.SourceCount;
import com.origin.onlinesys.persistence.modules.interviewsys.InterviewerInfoMapper;
import com.origin.onlinesys.service.modules.interviewsys.IinterviewerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/1/22 0022.
 * 面试者
 */
@Service
public class InterviewerInfoServiceImpl extends BaseService<InterviewerInfo> implements IinterviewerInfoService{
    @Autowired
    private InterviewerTraceServiceImpl service;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 插入面试者
     * @param interInfo 面试者对象
     * @return
     */
    public InterviewerInfo insertInterv(InterviewerInfo interInfo){
        interInfo.setState("1");
        this.save(interInfo);
        return interInfo;
    }

    /**
     * 删除面试
     * @param id
     * @return
     */
    public int delteInterv(String id){
        InterviewerInfo info = new InterviewerInfo();
        info.setId(id);
        info.setState("2");
        return this.update(info);
    }

    /**
     * 根据电话号码查询是否数据
     * @param phoneNum
     * @return
     */
    public InterviewerInfo selectByPhoneNum(String phoneNum,String zoneId){
        Map map = new HashMap();map.put("phoneNum",phoneNum);map.put("zoneId",zoneId);
        return ((InterviewerInfoMapper)mapper).selectByPhoneNum(map);
    }

    /**
     * 修改面试者数据
     * @param info 面试者对象
     * @return
     */
    public int updateInterv(InterviewerInfo info){
        return this.update(info);
    }

    /**
     * 查询所有
     * @return
     */
    public List<InterviewerInfo> selectAllInterv(){
        return this.queryAll();
    }

    /**
     * 根据条件查询
     * @param info 面试者对象
     * @return
     */
    public List<InterviewerInfo> selectByWhere(InterviewerInfo info){
        return this.queryByWhere(info);
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public InterviewerInfo selectById(String id){
        InterviewerInfo info = new InterviewerInfo();
        info.setId(id);
        return this.selectByWhere(info).get(0);
    }

    /**
     * 根据面试老师的ID查询
     * @param id
     * @return
     */
    public List<InterviewerInfo> selectByUserId(String id){
        InterviewerInfo info = new InterviewerInfo();
        info.setUserId(id);
        return this.selectByWhere(info);
    }

    /**
     * 分页查询数据
     * @param info
     * @return
     */
//    public Map selectInterviewByPage(int pageNum,int pageSize,InterviewerInfo info){
//        Map map = new HashMap();
//        info.setState("1");
//        Page<InterviewerInfo> page = this.selectByPage(pageNum, pageSize, info);
//        map.put("list", page);
//        map.put("pageSize", page.getPages());
//        map.put("total", page.getTotal());
//        return map;
//    }

    /**
     * 条件的分页查询
     * @param pageNum   第几页
     * @param pageSize  每页显示多少条
     * @param userId    用Id
     * @param phoneNum  面人者电话
     * @param realName  真实姓名
     * @param zoneId    区域Id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param state     是否按状态查询
     * @return
     */
    public Map selectByJoinAndPage(String sitId,int pageNum, int pageSize, String userId, String phoneNum, String realName,
                                   String zoneId,String startDate,String endDate,String state,int time) {
        Map search = new HashMap();
        search.put("userId", userId);
        search.put("realName",realName);
        search.put("phoneNum",phoneNum);
        search.put("sitId",sitId);
        search.put("zoneId",zoneId);
        search.put("startDate",startDate);
        search.put("endDate",endDate);
        search.put("state",state);
        PageHelper.startPage(pageNum, pageSize, true);
        Page<InterviewerInfo> page = (Page)((InterviewerInfoMapper)mapper).selectByPage(search);
        long cha;
        if(time > 0){
            for(InterviewerInfo bean:page){
                try {
                    cha = new Date().getTime()
                            - sdf.parse(bean.getCreateTime()).getTime();
                    if(cha >= 1000*60*60*24*time){
                        bean.setTimeout(true);
                    }else{bean.setTimeout(false);}
                } catch (ParseException e) {
                }
            }
        }
        Map result = new HashMap();
        result.put("list", page);
        result.put("pageSize", page.getPages());
        result.put("total", page.getTotal());
        return result;
    }
    @Override
    public InterviewerInfo selectLatelyTrace(String id) {
        return ((InterviewerInfoMapper)mapper).selectLatelyTrace(id);
    }

    @Override
    public List<InterviewerInfo> excelSelectInterview(String userId,String zoneId,String startDate, String endDate) {
        Map map = new HashMap();map.put("zoneId",zoneId);map.put("userId",userId);
        map.put("startDate",startDate);map.put("endDate",endDate);
        return ((InterviewerInfoMapper)mapper).excelSelectInterview(map);
    }

    @Override
    public List<InterviewerInfo> selectTimeOut(List<String> list) {
        return ((InterviewerInfoMapper)mapper).selectTimeOut(list);
    }

    public List<InterviewerInfo> excelShowDate(String userId,String zoneId,String startDate,String endDate){
        Map search = new HashMap();
        search.put("userId", userId);
        search.put("realName",null);
        search.put("phoneNum",null);
        search.put("zoneId",zoneId);
        search.put("startDate",startDate);
        search.put("endDate",endDate);
        search.put("state","true");
        return ((InterviewerInfoMapper)mapper).selectByPage(search);
    }
    /**
     * 统计添加个数
     * @param startDate
     * @param endDate
     * @param userId
     * @return
     */
    public String selectCountAdd(String startDate, String endDate, String userId) {
        Map map = new HashMap();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("userId",userId);
        return ((InterviewerInfoMapper)mapper).selectCountAdd(map);
    }

    /**
     *  统计来源
     * @param startDate
     * @param endDate
     * @param userId
     * @return
     */
    public List<SourceCount> selectCountSource(String startDate, String endDate, String userId) {
        Map map = new HashMap();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("userId",userId);
//        System.err.println(JSON.toJSONString(((InterviewerInfoMapper) mapper).selectCountSource(map)));
        return ((InterviewerInfoMapper)mapper).selectCountSource(map);
    }

    public InterviewerInfo selectInfo(String id) {
        return ((InterviewerInfoMapper)mapper).selectInfo(id);
    }

    /**
     * 统计
     * @param startDate
     * @param endDate
     * @param userId
     * @return
     */
    public Map countAdd(String startDate,String endDate,String userId){
        Map map = new HashMap();
        map.put("add",this.selectCountAdd(startDate,endDate,userId));
        map.put("trace",service.selectCountAdd(startDate,endDate,userId));
        return map;
    }


}
